<?php
	ini_set('display_errors', true);
	require_once(dirname(__FILE__) . "/../controllers/squadra.php");
	require_once(dirname(__FILE__) . "/../controllers/lega.php");
	require_once(dirname(__FILE__) . "/../model/db_driver.php");
	session_start();
	if(User::getCurrentUser()==null || User::getCurrentUser()->getRuolo()<User::$ADMIN){
		echo "Errore: devi essere un amministratore per visualizzare questa pagina.<br />";
		die();
	}
	if(isset($_POST['IsPostBack'])){
		//salva i dettagli modificati
		if(isset($_POST['btnSaveDetails'])){
			$s = null;
			if($_POST['btnSaveDetails']=='Salva'){
				$s = SquadraDesc::getSquadra($_POST['hdId']);
			}elseif($_POST['btnSaveDetails']=='Nuovo'){
				$s = new Squadra(-1);
			}
			$s->setNome($_POST['txtNome']);
			$s->setBudget($_POST['txtBudget']);
			$s->setUrlImgLogo($_POST['txtLogo']);
			$s->setAllenatoreId($_POST['cmbAllenatore']);
			$s->save();
		}
	}

	if(isset($_POST["squadra"])){
		echo tabellaDettagli($_POST["squadra"]);
		die();
	}

	function tabellaSquadre(){
		$lega = -1;
		$stagione = "";
		if(isset($_REQUEST['cmbLeghe'])) $lega = $_REQUEST['cmbLeghe'];
		$squadre=null;
		if($lega==-1){
			$squadre = SquadraDesc::getTutteSquadre();
			$stagione = Calendario::getStagione();
		}else{
			$squadre = SquadraDesc::getSquadre($lega);
			$l = Lega::getLega($lega);
			$stagione = $l->getStagione();
		}	
		$tab= "
		<table style='width:80%' cellpadding='0' cellspacing='0' border='0'>
			<thead>
				<tr>
					<td class='headerRow'>Id</td>
					<td class='headerRow'>Nome</td>
					<td class='headerRow'>Budget</td>
					<td class='headerRow'>Immagine logo</td>
					<td class='headerRow'>Allenatore</td>
					<td class='headerRow'>&nbsp;</td>
					<td class='headerRow'>&nbsp;</td>
				</tr>
			</thead>
		";
		for ($i=0;$i<count($squadre); $i++){
			$alt = $i % 2;
			$row_style = "rigaLega alt_" . $alt;
			$tab = $tab . "
				<tr>
					<td class='$row_style'>" . $squadre[$i]->getId() ."</td>
					<td class='$row_style'>" . $squadre[$i]->getNome() ."</td>
					<td class='$row_style' style='text-align:right;'>" . $squadre[$i]->getBudget() ."</td>
					<td class='$row_style'>" . $squadre[$i]->getUrlImgLogo() . "</td>
					<td class='$row_style'>" . $squadre[$i]->getAllenatore() . "</td>
					<td class='cc $row_style' style='text-decoration:underline;'
							onclick='showTeamDetails(" . $squadre[$i]->getId() .", \"dvTeamDetail\")'>
						Dettagli
					</td>
					<td class='cc $row_style' style='text-decoration:underline;'
							onclick='showTeamPlayers(" . $squadre[$i]->getId() .",\"" . $stagione . "\",\"dvTeamPlayers\")'>
						Rosa
					</td>
				</tr>
			";
		}
		
		return $tab . "</table>";
	}
	
	function tabellaDettagli($id){
		$nome = "";
		$budget="";
		$logo = "";
		$allenatore="";
		$allenatoreId=-1;
		$titolo = "Crea una nuova squadra";
		$evento = "saveTeamDetails();";
		if($id>=0){
			$s = SquadraDesc::getSquadra($id);
			$nome = $s->getNome();
			$budget = $s->getBudget();
			$logo = $s->getUrlImgLogo();
			$allenatore = $s->getAllenatore();
			$allenatoreId = $s->getAllenatoreId();
			$titolo = "Modifica i dettagli della squadra";
		}else{
			$id = "Nuova";
			$evento = "newTeam();";
		}
		
		if($logo=="") $log = "&nbsp;";
		$details = "
		<table style='width:99%;font-size:10px;' cellpadding='0' cellspacing='0'>
			<tr>
				<td colspan='2' class='details' style='width:100%;text-align:center'>
					<p class='' style='color:green;font-weight:bold;font-size:14px;text-align:center;'> $titolo </p>
				</td>
			</tr>
			<tr>
				<td class='details ldetails'>Id</td>
				<td class='details'>$id<input type='hidden'  id='hdId'  name='hdId' value='$id' /></td>
			</tr>
			<tr>
				<td class='details ldetails'>Nome</td>
				<td class='details'><input type='text' class='txt' id='txtNome' name='txtNome' value='$nome' /></td>
			</tr>
			<tr>
				<td class='details ldetails'>Budget</td>
				<td class='details'><input type='text' class='txt' id='txtBudget' name='txtBudget' value='$budget' /> </td>
			</tr>
			<tr>
				<td class='details ldetails'>Logo squadra</td>
				<td class='details'><input type='text' class='txt' id='txtLogo' name='txtBudget' value='$logo' /> </td>
			</tr>
			<tr>
				<td class='details ldetails'>Allenatore</td>
				<td class='details'>
					" . getComboBoxAllenatore($allenatoreId) . "
				</td>
			</tr>
			<tr>
				<td colspan='2' style='height:40px;'><p>&nbsp;</p></td>
			</tr>
			<tr>
				<td align='center' stsyle='text-align:center;'><input type='button' name='btnSaveDetails' value='Salva' onclick=' $evento closeDetails(\"dvTeamDetail\");' /></td>
				<td style='text-align:right;'><input type='button' name='btnCanc' value='Chiudi' onclick='closeDetails(\"dvTeamDetail\")' /></td>
			</tr>
		</table>
		";
		return $details;
	}
	
	function getComboBoxAllenatore($id){
		$users = User::getUsers();
		$selected = "";
		$combo = "
			<select name='cmbAllenatore' id = 'cmbAllenatore' class='combo'>";
			foreach($users as $u){
				if(!$u->isAbilitato()) continue;
				if($u->getId() == $id) $selected = "selected = 'selected'"; else $selected="";
				$combo = $combo . "<option $selected value='" . $u->getId() . "' >" . $u->getCognome() . " " . substr($u->getNome(), 0,1) . ".</option>";
			}
		$combo = $combo . "</select>";
		return $combo;
	}
	
	function getComboLeghe(){
		$leghe = Lega::caricaLeghe();
		$sel_lega = -2;
		if(isset($_REQUEST['cmbLeghe'])) $sel_lega = $_REQUEST['cmbLeghe'];
		$selected="";
		$cmb = "
		<select id='cmbLeghe' style='width:200px;' name='cmbLeghe' onchange='loadTeamOfLeague()'>";
		foreach($leghe as $l){
			if($l->getLegaId()==$sel_lega) $selected="selected='selected'"; else $selected="";
			$cmb = $cmb . "<option $selected value='" . $l->getLegaId() . "'>" . $l->getNome() . "</option>";
		}
		if($sel_lega==-1) $selected="selected='selected'";
		$cmb = $cmb . "
			<option value='-1'>Tutte le squadre</option>
		</select>";
		return $cmb;
	}


?>
	<script type='text/javascript' language='javascript'>
		
	</script>

	<style>
		td.headerRow {
			background-color:rgb(187,204,255);
			color:rgb(23,128,128);
			font-size:15px;
			border-right: solid 1px rgb(23,128,128);
			border-top: solid 1px rgb(23,128,128);
		
			padding-left: 3px;
			padding-right:3px;
			font-weight:bold;
		}
		
		td.rigaLega  {
			color:black;
			font-size:12px;
			border-right: solid 1px rgb(23,128,128);
			border-top: solid 1px rgb(23,128,128);
			padding-left: 3px;
			padding-right:3px;
			
		}

		td.alt_0{
			background-color:rgb(207,219,236);
		}
		
		td.alt_1{
			background-color:rgb(227,233,255);		
		}
		
		td.details{
			border-bottom:ridge 1px gray;
			border-right:ridge 1px gray;
			padding-bottom:3px;
			padding-left:3px;
			padding-top:3px;
		}
		
		td.ldetails{
			width:20%;
			text-align:right;
			padding-right:5px;
		}
		
		input.txt{
			width:95%;
			font-size:10px;
		}

			
		select.combo{
			font-size:10px;
			width:95%;
		}
	</style>
	<p  class='adminTitle'>
		Gestione leghe
	</p>
	<div>
		Squadre della lega: <?php echo getComboLeghe() ?>
		<br />
		<br />
	</div>
	<div>
		<button id='btnNewSquadra' onclick='showTeamDetails(-1)'>Nuova Squadra</button>
	</div>
	<br />
	<br />
	<!-- stampa la tabella degli utenti -->
	<?php echo tabellaSquadre(); ?>
	
	<!-- dettagli dell'utente -->
	<div id='dvTeamDetail' style='background-color:#FAFAD2;display:none;width:400px; height:250px;border: solid 1px black;
		position:absolute;top:320px;left:450px'>

		
	</div>
	
	<div id='dvTeamPlayers' style='background-color:#FAFAD2;display:none;width:400px; height:470px;border: solid 1px black;
		position:absolute;top:220px;left:450px'>

		
	</div>
